package club.monkeywood.service.buyer.impl;

import club.monkeywood.dataobject.Seller;
import club.monkeywood.dataobject.SellerInfo;
import club.monkeywood.dataobject.SellerPic;
import club.monkeywood.dto.buyer.SupportDTO;
import club.monkeywood.repository.SellerInfoRepository;
import club.monkeywood.repository.SellerPicRepository;
import club.monkeywood.repository.SellerRepository;
import club.monkeywood.repository.SellerSupportRepository;
import club.monkeywood.service.buyer.SellerService;
import club.monkeywood.vo.buyer.SellerVO;
import club.monkeywood.vo.buyer.SupportVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private SellerRepository sellerRepository;
    @Autowired
    private SellerSupportRepository sellerSupportRepository;
    @Autowired
    private SellerPicRepository sellerPicRepository;
    @Autowired
    private SellerInfoRepository sellerInfoRepository;

    @Override
    public SellerVO getSeller(Integer sellerId) {

        Seller seller = sellerRepository.findById(sellerId).orElse(null);
        SellerVO sellerVO = new SellerVO();
        BeanUtils.copyProperties(seller, sellerVO);

        List<SupportDTO> supportDTOList = sellerSupportRepository.queryBySellerId(sellerId);
        List<SupportVO> supportVOList = supportDTOList.stream()
                                            .map( e-> {
                                                SupportVO supportVO = new SupportVO();
                                                BeanUtils.copyProperties(e, supportVO);
                                                return supportVO;
                                            })
                                            .collect(Collectors.toList());
        sellerVO.setSupports(supportVOList);

        List<SellerPic> sellerPicList = sellerPicRepository.findBySellerId(sellerId);
        List<String> sellerPics = sellerPicList.stream()
                                    .map(e->e.getUrl())
                                    .collect(Collectors.toList());
        sellerVO.setPics(sellerPics);

        List<SellerInfo> sellerInfoList = sellerInfoRepository.findBySellerId(sellerId);
        List<String> sellerInfos = sellerInfoList.stream()
                                    .map(e->e.getInfo())
                                    .collect(Collectors.toList());
        sellerVO.setInfos(sellerInfos);

        return sellerVO;
    }
}
